package com.eqdd.yiqidian.ui.companyspace

import android.app.Activity
import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.widget.ImageView
import com.coorchice.library.SuperTextView
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector
import com.eqdd.yiqidian.base.BaseActivity
import com.eqdd.yiqidian.base.Config
import com.eqdd.yiqidian.bean.CompanySpaceBean
import com.eqdd.yiqidian.bean.rebuild.RowsResultBean
import com.eqdd.yiqidian.box.ItemDecoration.DividerLineItemDecoration
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource
import com.eqdd.yiqidian.helper.mvchelper.Rx2DataSource
import com.eqdd.yiqidian.http.HttpConfig
import com.eqdd.yiqidian.http.HttpResult
import com.eqdd.yiqidian.http.JsonConverter
import com.eqdd.yiqidian.kotlin.listImagePreview
import com.eqdd.yiqidian.ui.mine.space.OwnerComSpaceActivity
import com.eqdd.yiqidian.ui.yiqiyue.ArticleInfoActivity
import com.eqdd.yiqidian.utils.TimeUtil
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.HttpParams
import com.lzy.okrx2.adapter.FlowableBody
import com.shizhefei.mvc.MVCCoolHelper
import io.reactivex.Flowable
import kotlinx.android.synthetic.main.activity_recyclerview_refresh.*
import org.jetbrains.anko.startActivity
import java.util.*

/**
 * Created by jhj on 18-6-19.
 */

class CompanySpaceListActivity : BaseActivity() {

    var mvcHelper: MVCCoolHelper<List<CompanySpaceBean>>? = null
    private var pageNum: Int = 0


    override fun initBinding() {
        setContentView(R.layout.activity_recyclerview_refresh)
        initTopTitleBar(View.VISIBLE, "企业空间")
    }

    override fun initData() {
        val list = arrayListOf<CompanySpaceBean>()
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.addItemDecoration(DividerLineItemDecoration(this))
        val slimAdapterEx = SlimAdapterEx.create().register<CompanySpaceBean>(R.layout.list_item_149, SlimInjector<CompanySpaceBean> { data, injector ->
            injector?.imageCircle(R.id.iv_head, data?.iphoto)
                    ?.clicked(R.id.iv_head) {
                        startActivity<OwnerComSpaceActivity>(Config.COMPANY_ID to data?.companyId, Config.TYPE to 1)
                    }
                    ?.text(R.id.stv_name, data?.upname)
                    ?.text(R.id.tv_depart, TimeUtil.formatWebTime(data?.createTime))
                    ?.gone(R.id.iv_option)
                    ?.gone(R.id.tv_time)
                    ?.text(R.id.tv_title, data?.title)
                    ?.text(R.id.tv_content, data?.content)
                    ?.with<ImageView>(R.id.iv_image) { view ->
                        val v = view as ImageView
                        if (data?.image != null && "" != data.image) {
                            v.visibility = View.VISIBLE
                            listImagePreview(data.image, v)
                        } else {
                            v.visibility = View.GONE
                        }
                    }
                    ?.with<SuperTextView>(R.id.stv_see) { view ->
                        val v = view as SuperTextView
                        if (data?.browseCount ?: 0 > 0) {
                            v.text = data?.browseCount.toString()
                        } else {
                            v.text = "查看"
                        }
                    }
                    ?.with<SuperTextView>(R.id.stv_zan) { view ->
                        val v = view as SuperTextView
                        if (data?.zanCount ?: 0 > 0) {
                            v.text = data?.zanCount.toString()
                        } else {
                            v.text = "赞"
                        }
                    }
                    ?.with<SuperTextView>(R.id.stv_comment) { view ->
                        val v = view as SuperTextView
                        if (data?.commentCount ?: 0 > 0) {
                            v.text = data?.commentCount.toString()
                        } else {
                            v.text = "评论"
                        }
                    }
        }).attachTo(recyclerView).updateData(list as List<CompanySpaceBean>?) as SlimAdapterEx<CompanySpaceBean>
        ItemClickSupport.addTo(recyclerView).setOnItemClickListener { recyclerView, position, v ->
            startActivity<ArticleInfoActivity>(Config.ID to slimAdapterEx.getDataItem(position).id)
        }


        val dataSource = ModelRx2DataSource<CompanySpaceBean>(object : ModelRx2DataSource.OnLoadSource<CompanySpaceBean> {
            override fun loadSource(page: Int, register: Rx2DataSource.DoneActionRegister<MutableList<CompanySpaceBean>>?): Flowable<MutableList<CompanySpaceBean>> {
                pageNum = if (page == 1) 0 else pageNum
                val type = intent.getIntExtra(Config.TYPE, 0)
                val url: String
                val params: HttpParams = HttpParams()
                if (type == 1) {
                    url = HttpConfig.BASE_URL + HttpConfig.COM_SPACE_LIST //单个某企业
                    params.put("companyId", intent.getIntExtra(Config.COMPANY_ID, -1))
                } else {
                    url = HttpConfig.BASE_URL + HttpConfig.QIYE_ARTICLE //所有的
                    params.put("companyId", user.companyId)
                }
                return OkGo.post<HttpResult<RowsResultBean<CompanySpaceBean>>>(url)
                        .params("page", pageNum)
                        .params(params)
                        .params("departId", user.departId)
                        .converter(object : JsonConverter<HttpResult<RowsResultBean<CompanySpaceBean>>>() {
                            override fun test() {
                                super.test()
                            }
                        }).adapt(FlowableBody())
                        .flatMap { noticeWraperResultBeanHttpResult ->
                            pageNum = noticeWraperResultBeanHttpResult.items.page
                            Flowable.just(if (noticeWraperResultBeanHttpResult.items.rows == null) ArrayList() else noticeWraperResultBeanHttpResult.items.rows)
                        }

            }
        }, 10)

        mvcHelper = MVCCoolHelper(coolRefreshView)
        mvcHelper?.setDataSource(dataSource)
        mvcHelper?.setAdapter(slimAdapterEx)
        mvcHelper?.refresh()

    }

    override fun setView() {

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == Config.ADD && resultCode == Activity.RESULT_OK) {
            mvcHelper?.refresh()
        }
    }

}
